var mysql = require('mysql');
const request = require('request');
let config = {
    host: '119.29.24.107',
    user: 'api_qolayapp_cn',
    password: 'Hb8DJmbc5M2zR7P4',
    database: 'api_qolayapp_cn'
}
/* let config = {
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'cauth'
} */
let pool = mysql.createPool(config);
// let connection = mysql.createConnection(config)
/* exports.Mysql = function (sql) {
    return new Promise((resolve) => {
        connection.connect();
        connection.query(sql, function (err, results) {
            if (err) throw err
            else {
                resolve(results)
            }
        })
        connection.end()
    });
} */
exports.Mysql = function (sql) {
    return new Promise((resolve, reject) => {
        pool.getConnection(function (err, connection) {
            if (err) {
                reject(err)
            }
            connection.query(sql, function (error, results, fields) {
                //将链接返回到连接池中，准备由其他人重复使用
                connection.release();
                if (error) throw error;
                //执行回调函数，将数据返回
                resolve(results)
            });
        });
    })
}
// 字符串解密加密
exports.encode = function (str, key) {
    var length = key.length;
    var keyList = key.split("");
    var s = "",
        bit, bit1, bit2, bit3, bit4;
    for (var i = 0; i < str.length; i++) {
        bit = str.charCodeAt(i); //获得字符的Unicode 编码(数字)
        bit1 = bit % length;
        bit = (bit - bit1) / length;
        bit2 = bit % length;
        bit = (bit - bit2) / length;
        bit3 = bit % length;
        bit = (bit - bit3) / length;
        bit4 = bit % length;
        //bit1,bit2,bit3,bit4 key秘钥的位置
        //keyList[bit4]+keyList[bit3]+keyList[bit2]+keyList[bit1] 将str中的每一个字符对应秘钥的四个字符
        s += keyList[bit4] + keyList[bit3] + keyList[bit2] + keyList[bit1];
    }
    return s;
}

function dec(str, key) {
    var length = key.length;
    var bit, bit1, bit2, bit3, bit4, j = 0,
        s;
    var s = new Array(Math.floor(str.length / 4));
    var result = [];
    bit = s.length;
    for (var i = 0; i < bit; i++) {
        bit1 = key.indexOf(str.charAt(j));
        j++;
        bit2 = key.indexOf(str.charAt(j));
        j++;
        bit3 = key.indexOf(str.charAt(j));
        j++;
        bit4 = key.indexOf(str.charAt(j));
        j++;
        //bit1,bit2,bit3,bit4 每四个秘钥字符的位置 对应的是str的一个字符         
        s[i] = bit1 * length * length * length + bit2 * length * length + bit3 * length + bit4;
        //bit1*length*length*length+bit2*length*length+bit3*length+bit4还原str每个字符的Unicode 编码
        result.push(String.fromCharCode(s[i])); //将Unicode 编码还原数据
    }
    //还原字符
    return result.join("");
}


exports.encrypt = function (str, pwd = 'yusupturak') {
    if (pwd == null || pwd.length <= 0) {
        console.log("Please enter a password with which to encrypt the message.");
        return null;
    }
    var prand = "";
    for (var i = 0; i < pwd.length; i++) {
        prand += pwd.charCodeAt(i).toString();
    }
    var sPos = Math.floor(prand.length / 5);
    var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
    var incr = Math.ceil(pwd.length / 2);
    var modu = Math.pow(2, 31) - 1;
    if (mult < 2) {
        console.log("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
        return null;
    }
    var salt = Math.round(Math.random() * 1000000000) % 100000000;
    prand += salt;
    while (prand.length > 10) {
        prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
    }
    prand = (mult * prand + incr) % modu;
    var enc_chr = "";
    var enc_str = "";
    for (var i = 0; i < str.length; i++) {
        enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
        if (enc_chr < 16) {
            enc_str += "0" + enc_chr.toString(16);
        } else enc_str += enc_chr.toString(16);
        prand = (mult * prand + incr) % modu;
    }
    salt = salt.toString(16);
    while (salt.length < 8) salt = "0" + salt;
    enc_str += salt;
    return enc_str;
}

exports.decrypt = function (str, pwd = 'yusupturak') {
    if (str == null || str.length < 8) {
        console.log("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
        return;
    }
    if (pwd == null || pwd.length <= 0) {
        console.log("Please enter a password with which to decrypt the message.");
        return;
    }
    var prand = "";
    for (var i = 0; i < pwd.length; i++) {
        prand += pwd.charCodeAt(i).toString();
    }
    var sPos = Math.floor(prand.length / 5);
    var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
    var incr = Math.round(pwd.length / 2);
    var modu = Math.pow(2, 31) - 1;
    var salt = parseInt(str.substring(str.length - 8, str.length), 16);
    str = str.substring(0, str.length - 8);
    prand += salt;
    while (prand.length > 10) {
        prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
    }
    prand = (mult * prand + incr) % modu;
    var enc_chr = "";
    var enc_str = "";
    for (var i = 0; i < str.length; i += 2) {
        enc_chr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
        enc_str += String.fromCharCode(enc_chr);
        prand = (mult * prand + incr) % modu;
    }
    return enc_str;
}